/** 
 * @Title: Test.java 
 * @Package com.zizo.autohome.sentence.dao 
 * @Description: TODO
 * @author HeYan
 * @date 2016年12月14日 下午9:01:52 
 */
package com.zizo.autohome.sentence.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.zizo.autohome.emotion.alg.AhoCorasickDoubleArrayTrie;
import com.zizo.autohome.sentence.util.SentenceUtil;

/**
 * @ClassName: Test
 * @Description: TODO
 * @author HeYan
 * @date 2016年12月14日 下午9:01:52
 * 
 */
public class Test {
	private Map<Integer, List<Integer>> topics;// 所有主题集合的ID
	private Map<Integer, List<String>> topicMap;// 所有主题的集合
	private AhoCorasickDoubleArrayTrie<Integer> acdatLocal;
	private Map<Integer, List<Integer>> topics2;// 所有主题集合的ID
	private Map<Integer, List<String>> topicMap2;// 所有主题的集合
	private AhoCorasickDoubleArrayTrie<Integer> acdatLocal2;

	/**
	 * @Title: checkSub
	 * @Description: 去除子集操做
	 * @param list
	 * @return
	 * @return List<Integer>
	 * @author Alexis
	 */
	public final List<Integer> checkSub(List<Integer> list, int type) {
		List<Integer> res = new ArrayList<>();
		res.addAll(list);
		Map<Integer, List<String>> map = type == 1 ? topicMap : topicMap2;
		for (int i = 0; i < list.size(); i++) {
			for (int j = i + 1; j < list.size(); j++) {
				List<String> s1 = map.get(list.get(i));
				List<String> s2 = map.get(list.get(j));
				int l1 = 0;
				int l2 = 0;
				StringBuffer sb1 = new StringBuffer();
				StringBuffer sb2 = new StringBuffer();
				for (String s : s1) {
					l1 += s.length();
					sb1.append(s);
				}
				for (String s : s2) {
					l2 += s.length();
					sb2.append(s);
				}
				if (l1 >= l2) {
					boolean flag = true;
					String c = sb1.toString();
					for (String s : s2) {
						flag = c.contains(s);
						if (!flag) {
							break;
						}
					}
					if (flag) {
						res.remove((Integer) list.get(j));
					}
				} else {
					boolean flag = true;
					String c = sb2.toString();
					for (String s : s1) {
						flag = c.contains(s);
						if (!flag) {
							break;
						}
					}
					if (flag) {
						res.remove((Integer) list.get(i));
					}
				}
				s1 = null;
				s2 = null;
			}
		}
		return res;
	}

	/**
	 * @Title: isContains
	 * @Description: TODO 判断给定的句子是否包含特定的关键词，包含则插入到数据库中
	 * @param content
	 * @param year
	 * @return void
	 * @author Alexis
	 */
	public void isContains(String content) {
		List<Integer> ids = acdatLocal.parseTextNotHit(content);
		if (ids.size() < 1) {
			return;
		}
		List<Integer> idsAfterWord1 = getHitForWord(ids, content, 1);// 去除子集处理
		if (idsAfterWord1 == null) {
			return;
		}
		List<Integer> ids2 = acdatLocal2.parseTextNotHit(content);
		List<Integer> idsAfterWord2 = getHitForWord(ids2, content, 2);
		if (ids2.size() < 1) {
			return;
		}
		if (idsAfterWord2 == null) {
			return;
		}
		int size1 = idsAfterWord1.size();
		int size2 = idsAfterWord2.size();
		for (int i = 0; i < size1; i++) {
			List<String> l = topicMap.get(idsAfterWord1.get(i));
			for (String s : l) {
				if (s.length() > 0) {
					System.out.println(s);
				}
			}
		}
		for (int i = 0; i < size2; i++) {
			List<String> l = topicMap2.get(idsAfterWord2.get(i));
			for (String s : l) {
				if (s.length() > 0) {
					System.out.println(s);
				}
			}
		}
		idsAfterWord1 = null;
		idsAfterWord2 = null;

	}

	public List<Integer> getHitForWord(List<Integer> ids, String content, int type) {
		if (ids.size() == 0) {
			ids = null;
			return null;
		}
		Map<Integer, List<Integer>> topicInt = type == 1 ? topics : topics2;
		Set<Integer> set = new HashSet<>();
		for (int s : ids) {
			Set<Integer> ll = new HashSet<>(topicInt.get(s));
			set.addAll(ll);
		}
		ids = null;
		List<Integer> idList = new ArrayList<>(set);
		set = null;
		List<Integer> idTemp = new ArrayList<>(idList);
		Map<Integer, List<String>> topicString = type == 1 ? topicMap : topicMap2;
		for (int i : idTemp) {
			List<String> word = topicString.get(i);
			boolean flag = true;
			for (String w : word) {
				flag = content.contains(w);
				if (!flag) {
					break;
				}
			}
			if (!flag) {
				idList.remove((Integer) i);
			}
			word = null;
		}
		idTemp = null;
		List<Integer> hitList = checkSub(idList, type);
		idList = null;
		topicInt = null;
		topicString = null;
		return hitList;
	}

	/**
	 * @param topics
	 * @param topicMap
	 * @param acdatLocal
	 * @param topics2
	 * @param topicMap2
	 * @param acdatLocal2
	 */
	public Test(Map<Integer, List<Integer>> topics, Map<Integer, List<String>> topicMap,
			AhoCorasickDoubleArrayTrie<Integer> acdatLocal, Map<Integer, List<Integer>> topics2,
			Map<Integer, List<String>> topicMap2, AhoCorasickDoubleArrayTrie<Integer> acdatLocal2) {
		super();
		this.topics = topics;
		this.topicMap = topicMap;
		this.acdatLocal = acdatLocal;
		this.topics2 = topics2;
		this.topicMap2 = topicMap2;
		this.acdatLocal2 = acdatLocal2;
	}

	public static void main(String[] args) throws IOException {
		SentenceUtil sen = new SentenceUtil();
		String wordpath = "";
		List<List<String>> words = sen.readWord(wordpath, 4);
		int size = words.size();
		Map<String, Integer> wordMap = new HashMap<String, Integer>();// word---对应的所有主题集合的ID
		Map<Integer, List<Integer>> topics = new HashMap<Integer, List<Integer>>();// 所有主题集合的ID
		Map<Integer, List<String>> topicMap = new HashMap<>();// 所有主题的集合
		int index = 0;
		for (int i = 0; i < size; i++) {
			List<String> topic = new ArrayList<>();
			List<String> word = words.get(i);
			for (String s : word) {
				topic.add(s);
				if (s.length() > 0) {
					if (wordMap.containsKey(s)) {
						int key = wordMap.get(s);
						List<Integer> value = topics.get(key);
						value.add(i);
						topics.put(key, value);
					} else {
						List<Integer> list = new ArrayList<>();
						list.add(i);
						wordMap.put(s, index);
						topics.put(index, list);
						index++;
					}
				}
			}
			topicMap.put(i, topic);
			topic = null;
		}
		/* 双Trie树 */
		AhoCorasickDoubleArrayTrie<Integer> acdatLocal = new AhoCorasickDoubleArrayTrie<Integer>();
		acdatLocal.build(wordMap);

		List<List<String>> words2 = sen.readWord2(wordpath, 4);
		int size2 = words2.size();
		Map<String, Integer> wordMap2 = new HashMap<String, Integer>();// word---对应的所有主题集合的ID
		Map<Integer, List<Integer>> topics2 = new HashMap<Integer, List<Integer>>();// 所有主题集合的ID
		Map<Integer, List<String>> topicMap2 = new HashMap<>();// 所有主题的集合
		index = 0;
		for (int i = 0; i < size2; i++) {
			List<String> topic = new ArrayList<>();
			List<String> word = words2.get(i);
			for (String s : word) {
				topic.add(s);
				if (s.length() > 0) {
					if (wordMap2.containsKey(s)) {
						int key = wordMap2.get(s);
						List<Integer> value = topics2.get(key);
						value.add(i);
						topics2.put(key, value);
					} else {
						List<Integer> list = new ArrayList<>();
						list.add(i);
						wordMap2.put(s, index);
						topics2.put(index, list);
						index++;
					}
				}
			}
			topicMap2.put(i, topic);
			topic = null;
		}
		/* 双Trie树 */
		AhoCorasickDoubleArrayTrie<Integer> acdatLocal2 = new AhoCorasickDoubleArrayTrie<Integer>();
		acdatLocal2.build(wordMap2);

		Test test = new Test(topics, topicMap, acdatLocal, topics2, topicMap2, acdatLocal2);
		test.isContains("高速路的时候行车电脑的平均油耗显示5.8左右");
	}
}
